package com.aicloud.movies.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileUploadController {

	@PostMapping("/movies/upload")
	public @ResponseBody String upload(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
		if (file != null) {
			byte[] bytes = file.getBytes();
			File fileToSave = new File(file.getOriginalFilename());
			FileCopyUtils.copy(bytes, fileToSave);
			return fileToSave.getAbsolutePath();
		}
		return "file is null";

	}

}
